<template>
  <a-form layout="vertical" :form="form" style="max-width: 320px;margin-top: 12px">
    <div class="title">基础设置</div>
    <a-form-item
      style="display: none;"
    >
      <a-input v-decorator="['id']"/>
    </a-form-item>

    <a-form-item
      label="昵称"
    >
      <a-input placeholder="给自己起个昵称吧" v-decorator="['nickname']"/>
    </a-form-item>
    <a-form-item
      label="电子邮件"
      :required="false"
    >
      <a-input placeholder="请输入电子邮件地址"
               v-decorator="['email', {type: 'email',message: '请输入正确的邮箱号',rules: [{required: false,
                     message: '请输入正确的邮箱号！'}]}]"/>
    </a-form-item>
    <a-form-item>
      <a-button type="primary" @click="submitUserInfo">更新基本信息</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
  import store from '@/store'
  import { mapGetters } from 'vuex'
  import { updatePartObj } from '@/api/system/userManage'

  export default {
    data() {
      return {
        form: this.$form.createForm(this)
      }
    },
    computed: {
      ...mapGetters(['userInfo'])
    },
    mounted() {
      this.initUserInfo()
    },
    methods: {
      /**
       * 初始化用户信息
       */
      initUserInfo() {
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: this.userInfo.id,
              nickname: this.userInfo.nickname,
              email: this.userInfo.email
            }
          )
        }, 100)
      },
      submitUserInfo() {
        const { form: { validateFields } } = this
        validateFields((err, values) => {
          if (!err) {
            updatePartObj(values).then((res) => {
              if (res.success) {
                this.$message.success('个人信息更新成功')
                store.dispatch('GetInfo').then(() => {
                })
              } else {
                this.$message.error(res.message)
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
